package com.be.beadmin.device.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.dto.output.InspectSubItemExcelEx;
import com.be.beadmin.device.entity.InspectSubItem;
import com.be.beadmin.device.dto.output.InspectSubItemEx;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import com.be.beadmin.device.service.InspectSubItemService;
import com.be.beadmin.device.dto.input.InspectSubItemQueryCriteria;
import com.be.beadmin.device.dto.input.InspectSubItemDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author chenxu
* @date 2023-10-06
**/
@RestController
public class InspectSubItemController {

    @Autowired
    private  InspectSubItemService inspectSubItemService;


    @PostMapping(value = "/device/getInspectSubItemByPage")
    public ResultModel getInspectSubItemByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(inspectSubItemService.getInspectSubItemByPage(pageEntity)) ;
    }

    @PostMapping("/device/addInspectSubItem")
    public ResultModel addInspectSubItem(@RequestBody InspectSubItemDto inspectSubItemDto){
        inspectSubItemService.addInspectSubItem(inspectSubItemDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delInspectSubItem")
    public ResultModel delInspectSubItem(@RequestBody InspectSubItemDto inspectSubItemDto){
        inspectSubItemService.delInspectSubItem(inspectSubItemDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateInspectSubItem")
    public ResultModel updateInspectSubItem(@RequestBody InspectSubItemDto inspectSubItemDto){
        inspectSubItemService.updateInspectSubItem(inspectSubItemDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @PostMapping("/device/exportInspectSubItem")
    public void exportInspectSubItem(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<InspectSubItemEx> inspectSubItemPage = inspectSubItemService.getInspectSubItemByPage(pageEntity);
        List<InspectSubItemEx> inspectSubItemExes = inspectSubItemPage.getContent();
        List<InspectSubItemExcelEx> inspectSubItemExcelExes=inspectSubItemExes.stream().map(item->{
            InspectSubItemExcelEx excel=new InspectSubItemExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("InspectSubItem接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), InspectSubItemExcelEx.class).sheet("sheet1").doWrite(inspectSubItemExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), InspectSubItemExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(inspectSubItemExcelExes);
    }
}